<?php namespace App\Http\Controllers\user\industry;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

use App\Models\Projects;
use App\Models\Devices;
use App\Models\Technologies;
use App\Models\Users;

class IndexController extends Controller {

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index()
	{
		$project = Projects::with(["user", "investmentType"])->where("verify_state", 1)->where('is_rec_industry_index', 1)->orderBy("industry_order", "desc")->take(3)->get();
		foreach($project as $model){
			switch($model->user->role_id){
				case env("ENERGY"):
					$model->load("user.poweruser");
					$model["userData"] = $model["user"]["poweruser"];
					unset($model["user"]["poweruser"]);
				break;
				case env("SAVER"):
					$model->load("user.energysaver");
					$model["userData"] = $model["user"]["energysaver"];
					unset($model["user"]["energysaver"]);
				break;
				case env("DEVICE_TECH"):
					$model->load("user.devicetech");
					$model["userData"] = $model["user"]["devicetech"];
					unset($model["user"]["devicetech"]);
				break;
				case env("THIRD"):
					$model->load("user.thirdparty");
					$model["userData"] = $model["user"]["thirdparty"];
					unset($model["user"]["thirdparty"]);
				break;
				case env("ASSOCIATION"):
					$model->load("user.association");
					$model["userData"] = $model["user"]["association"];
					unset($model["user"]["association"]);
				break;
				case env("GOVERNMENT"):
					$model->load("user.government");
					$model["userData"] = $model["user"]["government"];
					unset($model["user"]["government"]);
				break;
			}
		}
		$all = Projects::where("verify_state", 1)->get();
		$count = $all->count();
		$total = $all->sum("investment_scale");

		$technologies = Technologies::with(["techDomain", "techDomain.domain", "techDomain.domain.parent"])->where("verify_state", 1)->where('is_rec_industry_index', 1)->orderBy("industry_order", "desc")->take(3)->get();
		$allTech = Technologies::where("verify_state", 1)->get();
		$techCount = $allTech->count();

		$devices = Devices::with(["user", "user.userRole", "deviceDomain", "deviceDomain.domain", "deviceDomain.domain.parent"])->where("verify_state", 1)->where('is_rec_industry_index', 1)->orderBy("industry_order", "desc")->take(3)->get();
		foreach($devices as $model){
			$model["userRole"] = $model["user"]["userRole"];
			unset($model["user"]["userRole"]);
			switch($model->user->role_id){
				case env("ENERGY"):
					$model->load("user.poweruser");
					$model["userData"] = $model["user"]["poweruser"];
					unset($model["user"]["poweruser"]);
				break;
				case env("SAVER"):
					$model->load("user.energysaver");
					$model["userData"] = $model["user"]["energysaver"];
					unset($model["user"]["energysaver"]);
				break;
				case env("DEVICE_TECH"):
					$model->load("user.devicetech");
					$model["userData"] = $model["user"]["devicetech"];
					unset($model["user"]["devicetech"]);
				break;
				case env("THIRD"):
					$model->load("user.thirdparty");
					$model["userData"] = $model["user"]["thirdparty"];
					unset($model["user"]["thirdparty"]);
				break;
				case env("ASSOCIATION"):
					$model->load("user.association");
					$model["userData"] = $model["user"]["association"];
					unset($model["user"]["association"]);
				break;
				case env("GOVERNMENT"):
					$model->load("user.government");
					$model["userData"] = $model["user"]["government"];
					unset($model["user"]["government"]);
				break;
			}
		}

		$allDevices = Devices::where("verify_state", 1)->get();
		$deviceCount = $allDevices->count();

		$users = Users::with("userRole")->where("validate_state", 2)->where("is_rec_industry_index", 1)->whereNotIn("role_id", [0, 1, 8, 9, 10])->orderBy("industry_order", "desc");
		$totalUser = Users::where("validate_state", 2)->whereNotIn("role_id", [0, 1, 8, 9, 10]);
		$totalUser = $totalUser->count();
		$users = $users->take(3)->get();

		foreach($users as $user){
			switch ($user["role_id"]) {
				case env("ENERGY"):
					$user->load("poweruser");
					$user["userData"] = $user["poweruser"];
					unset($user["poweruser"]);
					break;
				case env("SAVER"):
					$user->load("energysaver");
					$user["userData"] = $user["energysaver"];
					unset($user["energysaver"]);
					break;
				case env("DEVICE_TECH"):
					$user->load("devicetech");
					$user["userData"] = $user["devicetech"];
					unset($user["devicetech"]);
					break;
				case env("THIRD"):
					$user->load("thirdparty");
					$user["userData"] = $user["thirdparty"];
					unset($user["thirdparty"]);
					break;
				case env("ASSOCIATION"):
					$user->load("association");
					$user["userData"] = $user["association"];
					unset($user["association"]);
					break;
				case env("GOVERNMENT");
					$user->load("government");
					$user["userData"] = $user["government"];
					unset($user["government"]);
					break;
			}
		}
		// dd($users->toArray());
		return view('user.industry.index', [
			'project' => $project->toArray(),
			'count' => $count,
			'total' => $total,
			'techCount' => $techCount,
			'tech' => $technologies->toArray(),
			'deviceCount' => $deviceCount,
			'device' => $devices->toArray(),
			'userCount' => $totalUser,
			'users' => $users->toArray()
		])->withIsIndustry(true);
	}

	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create()
	{
		//
	}

	/**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function store()
	{
		//
	}

	/**
	 * Display the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		//
	}

	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		//
	}

	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		//
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		//
	}
}
